package com.jjlin.chapter_8;

import java.util.*;
public class CD21{

    public static long smallSum(int[] arr, int l, int r){
        if(l == r || arr.length < 2)
            return 0;
        int mid = l + ((r - l) >> 1);//注意括号
        return smallSum(arr, l, mid)
                + smallSum(arr, mid + 1, r) + merge(arr, l, mid, r);
    }

    public static long merge(int[] arr, int l, int mid, int r){
        int[] help = new int[r - l + 1];
        long res = 0;
        int p1 = l, p2 = mid + 1;
        int i = 0;
        while(p1 <= mid && p2 <= r){
            //左边的数等于右边的数也算是一个小和
            res += arr[p1] <= arr[p2] ? arr[p1] * (r - p2 + 1) : 0;
            help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];
        }
        while(p1 <= mid){
            help[i++] = arr[p1++];
        }
        while(p2 <= r){
            help[i++] = arr[p2++];
        }
        for(i = 0; i < help.length; ++i)
            arr[l + i] = help[i];
        return res;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; ++i)
            arr[i] = sc.nextInt();
        System.out.println(smallSum(arr, 0, n - 1));
    }
}